// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Water Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 40 : TYPE_WATER

// Function declarations
reserve function Water_Function103
reserve function Water_Function104
reserve function Water_Function105
reserve function Water_Function106
reserve function Water_Function107
reserve function Water_Function108
reserve function Water_Function109

// Static Values
public value Water_value31 = 0;
public value Water_value32 = 0;

// Tables
private table Water_table26
	0, 0, 0, 0, 0
end table

function Water_Function103
	switch stage.waterState
	case 0
		stage.newWaterLevel = 0xB80000
		if temp0 >= 0x6A0
			stage.newWaterLevel = 0x1080000
			if temp1 < 512
				if temp0 >= 0xD20
					stage.newWaterLevel = 0xE80000
					if temp0 >= 0x15A0
						stage.newWaterLevel = 0x1080000
					end if
				end if
			else
				if temp0 >= 0xCA0
					stage.newWaterLevel = 0x3180000
					if temp0 >= 0x1120
						stage.newWaterLevel = 0x5C80000
						if temp0 >= 0x1420
							stage.newWaterLevel = 0x3A80000
							if object[arrayPos0].ypos == stage.newWaterLevel
								stage.waterState = 1
							end if
						end if
					end if
				end if
			end if
		end if
		break
	case 1
		if temp1 < 736
			stage.newWaterLevel = 0x3A80000
			if temp0 >= 0x13A0
				stage.newWaterLevel = 0x1080000
				stage.waterState = 2
			end if
		end if
		break
	case 2
		break
	end switch
end function


function Water_Function104
	stage.newWaterLevel = 0x3280000
	if temp0 >= 0x5A0
		stage.newWaterLevel = 0x3C80000
		if temp0 >= 0xBA0
			stage.newWaterLevel = 0x4280000
		end if
	end if
end function


function Water_Function105
	switch stage.waterState
	case 0
		stage.newWaterLevel = 0xA000000
		object[arrayPos0].ypos = stage.newWaterLevel
		if temp0 >= 0x6A0
			if temp1 >= 960
				if temp1 < 0x600
					stage.waterState = 1
					stage.newWaterLevel = 0x4C80000
					object[arrayPos0].ypos = stage.newWaterLevel
					PlaySfx(SfxName[Large Wall], 0)
					SetTileLayerEntry(302, 0, 12, 5)
					SetTileLayerEntry(303, 0, 13, 5)
				end if
			end if
		end if
		break
	case 1
		temp3 = 0x4C80000
		if temp0 >= 0x800
			temp3 = 0x3080000
			if temp0 >= 0x14A0
				temp2 = 0
				if stage.newWaterLevel == 0x5080000
					temp2 = 1
				else
					if temp1 >= 0x600
						temp2 = 1
					else
						if temp1 < 640
							temp2 = 1
						end if
					end if
				end if
				if temp2 == 1
					temp3 = 0x5080000
					object[arrayPos0].ypos = temp3
					if temp0 >= 0x1810
						stage.waterState = 2
					end if
				end if
			end if
		end if
		stage.newWaterLevel = temp3
		break
	case 2
		stage.newWaterLevel = 0x5080000
		if temp0 >= 0x1900
			stage.newWaterLevel = 0x1880000
			if temp0 < 0x1B80
				if object[arrayPos0].ypos == stage.newWaterLevel
					stage.waterState = 3
				end if
			else
				stage.waterState = 3
			end if
		end if
		break
	case 3
		stage.newWaterLevel = 0x1880000
		object[arrayPos0].ypos = stage.newWaterLevel
		if temp1 > 0x700
			stage.newWaterLevel = 0xA000000
			object[arrayPos0].ypos = stage.newWaterLevel
		end if
		if temp1 < 160
			stage.newWaterLevel = 0xA000000
			object[arrayPos0].ypos = stage.newWaterLevel
		end if
		if temp0 >= 0x1B80
			stage.newWaterLevel = 0xA000000
			object[arrayPos0].ypos = stage.newWaterLevel
			if temp0 >= 0x1C60
				stage.waterState = 4
				stage.newWaterLevel = 0x6080000
				object[arrayPos0].ypos = 0x7C00000
				Water_value32 = 1
			end if
		end if
		break
	case 4
		if temp0 >= 0x400
			stage.newWaterLevel = 0x1280000
		end if
		break
	end switch
end function


function Water_Function106
	stage.newWaterLevel = 0x2280000
	if temp0 >= 0xFA0
		stage.newWaterLevel = 0x4C80000
	end if
end function


function Water_Function107
	if object[+currentPlayer].value1 > 0
		object[+currentPlayer].value1--
		if object[+currentPlayer].value2 > 0
			object[+currentPlayer].value2--
		else
			CreateTempObject(TypeName[Air Bubble], 2, object[currentPlayer].xpos, object[currentPlayer].ypos)
			object[tempObjectPos].drawOrder = 4
			object[tempObjectPos].yvel = -0x8800
			object[tempObjectPos].value2 = currentPlayer
			if object[currentPlayer].direction == FLIP_NONE
				object[tempObjectPos].xpos += 0x60000
			else
				object[tempObjectPos].xpos -= 0x60000
				object[tempObjectPos].angle = 256
			end if
			object[tempObjectPos].value1 = object[tempObjectPos].xpos
			object[+currentPlayer].value2 = 512
		end if
	else
		Rand(temp0, 3)
		if temp0 == 1
			Rand(object[+currentPlayer].value2, 16)
			object[+currentPlayer].value2 += 8
		else
			object[+currentPlayer].value2 = 512
		end if
		object[+currentPlayer].value1 = 60
		CreateTempObject(TypeName[Air Bubble], 2, object[currentPlayer].xpos, object[currentPlayer].ypos)
		object[tempObjectPos].drawOrder = 4
		object[tempObjectPos].yvel = -0x8800
		object[tempObjectPos].value2 = currentPlayer
		if object[currentPlayer].direction == FLIP_NONE
			object[tempObjectPos].xpos += 0x60000
		else
			object[tempObjectPos].xpos -= 0x60000
			object[tempObjectPos].angle = 256
		end if
		object[tempObjectPos].value1 = object[tempObjectPos].xpos
	end if
end function


function Water_Function108
	CreateTempObject(TypeName[Countdown Bubble], temp0, object[currentPlayer].xpos, object[currentPlayer].ypos)
	object[tempObjectPos].drawOrder = 5
	object[tempObjectPos].yvel = -0x8800
	object[tempObjectPos].value2 = currentPlayer
	if object[currentPlayer].direction == FLIP_NONE
		object[tempObjectPos].xpos += 0x60000
	else
		object[tempObjectPos].xpos -= 0x60000
		object[tempObjectPos].angle = 256
	end if
	object[tempObjectPos].value1 = object[tempObjectPos].xpos
end function


function Water_Function109
	if currentPlayer == 0
		arrayPos0 = Water_value31
		arrayPos0 += currentPlayer
		if object[currentPlayer].type != TypeName[Death Event]
			if music.currentTrack == 6
				if object[arrayPos0].value8 == 2
					arrayPos1 = currentPlayer
					arrayPos1 += playerCount
					if object[arrayPos1].type != TypeName[Invincibility]
						if PlayerObject_SuperState != 1
							object[arrayPos0].value8 = 0
						end if
					end if
				end if
				PlayMusic(object[arrayPos0].value8)
			end if
		end if
	end if
end function


event ObjectMain
	temp0 = object[0].xpos
	temp0 >>= 16
	temp1 = object[0].ypos
	temp1 >>= 16
	arrayPos0 = object.entityPos
	CallFunction(object.value3)
	if object.ypos < stage.newWaterLevel
		object.ypos += 0x10000
	end if
	if object.ypos > stage.newWaterLevel
		object.ypos -= 0x10000
	end if
	temp0 = oscillation
	temp0 <<= 1
	Sin(stage.waterLevel, temp0)
	stage.waterLevel <<= 10
	stage.waterLevel += object.ypos
	temp7 = stage.waterLevel
	stage.waterLevel >>= 16
	currentPlayer = 0
	while currentPlayer < playerCount
		if object[currentPlayer].value25 == 0x3800
			CheckNotEqual(object[currentPlayer].type, TypeName[Debug Mode])
			temp0 = checkResult
			CheckNotEqual(object[currentPlayer].state, PlayerObject_Death)
			temp0 &= checkResult
			CheckGreater(object[currentPlayer].ypos, temp7)
			temp0 &= checkResult
			if temp0 == 1
				if object[currentPlayer].yvel > 0
					object[currentPlayer].yvel >>= 2
				end if
				if object.ypos < stage.newWaterLevel
					object[currentPlayer].yvel += 0x10000
				end if
				if object[currentPlayer].state == PlayerObject_HandleTailsFlight
					StopSfx(SfxName[Flying])
					StopSfx(SfxName[Jump])
				end if
				object[currentPlayer].xvel >>= 1
				object[currentPlayer].speed >>= 1
				CallFunction(PlayerObject_SwitchPhysicsState)
				if object[currentPlayer].yvel != 0
					CreateTempObject(TypeName[Water Splash], 0, object[currentPlayer].xpos, temp7)
					object[tempObjectPos].drawOrder = 4
					PlaySfx(SfxName[Water Splash], 0)
				end if
				object[+currentPlayer].value1 = 52
				object[currentPlayer].value3 = 0
			end if
		else
			if object[currentPlayer].value37 == 3
				temp2 = 0
				while temp2 < 8
					Rand(temp0, 32)
					Rand(temp1, 32)
					temp0 -= 16
					temp0 <<= 16
					temp0 += object[currentPlayer].xpos
					temp1 -= 16
					temp1 <<= 16
					temp1 += object[currentPlayer].ypos
					CreateTempObject(TypeName[Dust Puff], 0, temp0, temp1)
					object[tempObjectPos].drawOrder = object[currentPlayer].value18
					object[tempObjectPos].drawOrder++
					object[tempObjectPos].xvel = object[currentPlayer].xvel
					object[tempObjectPos].yvel = object[currentPlayer].yvel
					temp2++
				loop
				object[currentPlayer].value37 = 0
				arrayPos0 = currentPlayer
				arrayPos0 += playerCount
				CallFunction(PlayerObject_RestorePowerup)
			end if
			if object[currentPlayer].value37 == 4
				object[currentPlayer].value37 = 0
				arrayPos0 = currentPlayer
				arrayPos0 += playerCount
				CallFunction(PlayerObject_RestorePowerup)
				LZSetup_value29 = 4
			end if
			if object[currentPlayer].value37 != 2
				if object[currentPlayer].value3 == 0
					object[currentPlayer].value4 = 0
					CallFunction(Water_Function109)
				end if
				switch object[currentPlayer].value4
				case 0
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 360
						if currentPlayer == 0
							PlaySfx(SfxName[Drown Alert], 0)
						end if
						object[currentPlayer].value4++
					end if
					break
				case 1
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 660
						if currentPlayer == 0
							PlaySfx(SfxName[Drown Alert], 0)
						end if
						object[currentPlayer].value4++
					end if
					break
				case 2
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 960
						if currentPlayer == 0
							PlaySfx(SfxName[Drown Alert], 0)
						end if
						object[currentPlayer].value4++
					end if
					break
				case 3
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x438
						if currentPlayer == 0
							object.value8 = music.currentTrack
							if options.bossGame != 1
								PlayMusic(6)
							endif
						end if
						temp0 = 0
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 4
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x4B0
						temp0 = 1
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 5
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x528
						temp0 = 2
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 6
					if options.bossGame == 1
						Temp0=object[currentPlayer].value3
						Temp0&=63
						if Temp0==0
							PlaySfx(SfxName[Drown Alert CD], 0)
						end if
					endif
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x5A0
						temp0 = 3
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 7
					if options.bossGame == 1
						Temp0=object[currentPlayer].value3
						Temp0&=63
						if Temp0==0
							PlaySfx(SfxName[Drown Alert CD], 0)
						end if
					endif
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x618
						temp0 = 4
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 8
					if options.bossGame == 1
						Temp0=object[currentPlayer].value3
						Temp0&=63
						if Temp0==0
							PlaySfx(SfxName[Drown Alert CD], 0)
						end if
					endif
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x690
						temp0 = 5
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 9
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x708
						if object[currentPlayer].state != PlayerObject_Death
							if currentPlayer == 0
								stage.timeEnabled = 0
								screen.cameraEnabled = 0
							end if
							object[currentPlayer].state = PlayerObject_Drown
							object[currentPlayer].animation = ANI_DROWNING
							object[currentPlayer].speed = 0
							object[currentPlayer].xvel = 0
							object[currentPlayer].yvel = 0
							object[currentPlayer].tileCollisions = 0
							object[currentPlayer].interaction = 0
							object[currentPlayer].value18 = 5
							object[+currentPlayer].value1 = 2
							PlaySfx(SfxName[Drowning], 0)
							object[currentPlayer].value4++
						end if
					end if
					break
				case 10
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x744
						object[currentPlayer].value4++
					end if
					if object[+currentPlayer].value1 > 0
						object[+currentPlayer].value1--
						if object[+currentPlayer].value2 > 0
							object[+currentPlayer].value2--
						else
							Rand(temp1, 2)
							temp1 += 2
							CreateTempObject(TypeName[Air Bubble], temp1, object[currentPlayer].xpos, object[currentPlayer].ypos)
							object[tempObjectPos].drawOrder = 5
							object[tempObjectPos].yvel = -0x8800
							object[tempObjectPos].ypos -= 0x60000
							object[tempObjectPos].value2 = currentPlayer
							Rand(object[tempObjectPos].angle, 256)
							object[tempObjectPos].value1 = object[tempObjectPos].xpos
							object[+currentPlayer].value2 = 512
						end if
					else
						Rand(temp0, 5)
						if temp0 == 1
							object[+currentPlayer].value2 = 2
						else
							object[+currentPlayer].value2 = 512
						end if
						object[+currentPlayer].value1 = 6
						Rand(temp1, 2)
						temp1 += 2
						CreateTempObject(TypeName[Air Bubble], temp1, object[currentPlayer].xpos, object[currentPlayer].ypos)
						object[tempObjectPos].drawOrder = 5
						object[tempObjectPos].yvel = -0x8800
						object[tempObjectPos].ypos -= 0x60000
						object[tempObjectPos].value2 = currentPlayer
						Rand(object[tempObjectPos].angle, 256)
						object[tempObjectPos].value1 = object[tempObjectPos].xpos
					end if
					break
				end switch
			end if
			CheckEqual(object[currentPlayer].type, TypeName[Debug Mode])
			temp0 = checkResult
			CheckLower(object[currentPlayer].ypos, temp7)
			temp0 |= checkResult
			if temp0 == 1
				if object[currentPlayer].state != PlayerObject_Death
					object[currentPlayer].yvel <<= 1
					if object[currentPlayer].yvel < -0x100000
						object[currentPlayer].yvel = -0x100000
					end if
					if object[currentPlayer].state == PlayerObject_HandleTailsFlight
						if object[currentPlayer].value1 < 480
							PlaySfx(SfxName[Flying], 1)
						else
							PlaySfx(SfxName[Jump], 1)
						end if
					end if
					CallFunction(PlayerObject_SwitchPhysicsState)
					if object[currentPlayer].yvel != 0
						temp0 = object.ypos
						temp0 -= object[currentPlayer].ypos
						if temp0 < 0xF00000
							CreateTempObject(TypeName[Water Splash], 0, object[currentPlayer].xpos, temp7)
							object[tempObjectPos].drawOrder = 4
							PlaySfx(SfxName[Water Splash], 0)
						end if
					end if
					object[currentPlayer].value3 = 0
					CallFunction(Water_Function109)
				end if
			end if
		end if
		currentPlayer++
	loop
	object.animationTimer++
	object.animationTimer %= 24
	object.frame = object.animationTimer
	object.frame >>= 3
end event


event ObjectDraw
	temp0 = screen.xoffset
	Sin(temp1, oscillation)
	temp1 >>= 5
	temp0 += temp1
	temp0 &= 63
	FlipSign(temp0)
	temp1 = stage.waterLevel
	temp1 -= screen.yoffset
	DrawSpriteScreenFX(object.frame, FX_INK, temp0, temp1)
	temp0 += 256
	DrawSpriteScreenFX(object.frame, FX_INK, temp0, temp1)
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects2.gif")
	SetTableValue(Water_Function103, 1, Water_table26)
	SetTableValue(Water_Function104, 2, Water_table26)
	SetTableValue(Water_Function105, 3, Water_table26)
	SetTableValue(Water_Function106, 4, Water_table26)
	foreach (TypeName[Water], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		object[arrayPos0].drawOrder = 5
		object[arrayPos0].inkEffect = INK_ALPHA
		object[arrayPos0].alpha = 160
		if credits.screen == 0
			stage.newWaterLevel = object[arrayPos0].ypos
		else
			stage.waterState = 1
			stage.newWaterLevel = 0x4C80000
			temp0 = object[0].xpos
			temp0 >>= 16
			temp1 = object[0].ypos
			temp1 >>= 16
		end if
		Water_value31 = arrayPos0
		GetTableValue(object[arrayPos0].value3, stage.actNum, Water_table26)
		CallFunction(object[arrayPos0].value3)
		stage.waterLevel = stage.newWaterLevel
		stage.waterLevel >>= 16
		object[arrayPos0].ypos = stage.newWaterLevel
	next
	if starPostID > 31
		stage.waterState = recWaterState
		stage.waterLevel = recWaterLevel
		stage.newWaterLevel = recWaterLevel
		stage.newWaterLevel <<= 16
		arrayPos0 = Water_value31
		object[arrayPos0].ypos = stage.newWaterLevel
	else
		if credits.screen == 0
			stage.waterState = 0
		end if
	end if
	Water_value32 = 0
	//TODO: wtf? type of "256" while "ALL_ENTITIES", should this be ACTIVE_ENTITIES?
	foreach (GROUP_PLAYERS, currentPlayer, ALL_ENTITIES)
		object[currentPlayer].value3 = 0
		if object[currentPlayer].ypos > stage.newWaterLevel
			object[currentPlayer].yvel >>= 2
			object[currentPlayer].xvel >>= 1
			object[currentPlayer].speed >>= 1
			object[currentPlayer].value20 >>= 1
			object[currentPlayer].value21 >>= 1
			object[currentPlayer].value22 >>= 1
			object[currentPlayer].value23 >>= 1
			object[currentPlayer].value24 >>= 1
			object[currentPlayer].value25 = 0x1000
			if stage.playerListPos == 2
				object[currentPlayer].value27 = 0x30000
			else
				object[currentPlayer].value27 = 0x38000
			end if
			object[currentPlayer].value28 = -0x20000
			arrayPos0 = Water_value31
			arrayPos0 += currentPlayer
			object[arrayPos0].value1 = 52
		end if
	next
	SpriteFrame(0, -8, 256, 16, 0, 129)
	SpriteFrame(0, -8, 256, 16, 0, 146)
	SpriteFrame(0, -8, 256, 16, 0, 163)
//	BrokenMonitor_value25 = 109
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
